package com.ithe.ssmandmg.controller;
import com.ithe.core.result.Result;
import com.ithe.core.result.ResultCode;
import com.ithe.ssmandmg.service.SmsService;
import com.ithe.user.api.UserFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by zxd on 2021/1/7
 **/
@RequestMapping("/app-api/v1/sms")
@Controller
@ResponseBody
@Api(value = "短信相关接口文档")
public class SmsController {


    @Autowired
    private SmsService smsService;
    @Autowired
    private UserFeignClient userFeignClient;

    @ApiOperation(value = "用户短信注册接口",httpMethod = "GET")
    @GetMapping("/register/{phone}")
    public Result sendRegisterSms(@PathVariable("phone")String phone){
        //校验
        if(!phone.matches("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$")){
            return Result.failed(ResultCode.PHONE_ERROR);
        }
        if (userFeignClient.findUserByPhone(phone)!=null){
            return Result.failed(ResultCode.PHONE_EXIST);
        }
        smsService.sendRegisterSms(phone);
        return Result.success();
    }

    @RequestMapping(value = "/login")
    @ApiOperation(value = "发送一键登录短信",notes = "发送一键登录短信",httpMethod = "POST")
    public Result sendLoginSms(@RequestParam String phone) {
        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
            return  Result.failed(ResultCode.PHONE_ERROR);
        }
        smsService.sendLoginSms(phone);
        return Result.success();
    }


}
